/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Ltd.            */
/*                                                                 */
/*  NAME      = t_sqlserver_get_binary.c                          */
/*  NOTE      =                                                    */
/*  DATE      = 2015/01/14 by geyh                                 */
/*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <CUnit/Basic.h>

#include <frcomm.h>
#include <sqlserver/config.h>
#include <sqlserver/sqlserver_core.h>
#include <sqlserver/sqlserver_common.h>
#include <apmtest_comm.h>

extern int sqlserver_get_binary(Frbyte_Packet *p_st_data
                                , int i_whole_flg
                                , uint8_t u8_data_type
                                , Frdata_field **pp_st_value);

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void __case1(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_BINARY;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();

    i_ret = sqlserver_get_binary(p_st_data, i_whole_flg, u8_data_type, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    bytebuff_free(&p_st_data);
    return;
}

static void __case2(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_BINARY;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x01\x01\x08", 3);

    i_ret = sqlserver_get_binary(p_st_data, i_whole_flg, u8_data_type, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRBLOB);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "BLOB_DATA");

    bytebuff_free(&p_st_data);
    if (p_st_value && p_st_value->p_field_value)
        frfree(p_st_value->p_field_value);
    frfree(p_st_value);

    return;
}

static void __case3(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_VARBINARY;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x01\x01\x08", 3);

    i_ret = sqlserver_get_binary(p_st_data, i_whole_flg, u8_data_type, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRBLOB);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "BLOB_DATA");

    bytebuff_free(&p_st_data);
    if (p_st_value && p_st_value->p_field_value)
        frfree(p_st_value->p_field_value);
    frfree(p_st_value);

    return;
}

static void __case4(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_BIGVARBIN;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x01\x00\x01\x08", 4);

    i_ret = sqlserver_get_binary(p_st_data, i_whole_flg, u8_data_type, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRBLOB);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "BLOB_DATA");

    bytebuff_free(&p_st_data);
    if (p_st_value && p_st_value->p_field_value)
        frfree(p_st_value->p_field_value);
    frfree(p_st_value);

    return;
}

static void __case5(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_BIGBINARY;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x01\x00\x01\x08", 4);

    i_ret = sqlserver_get_binary(p_st_data, i_whole_flg, u8_data_type, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRBLOB);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "BLOB_DATA");

    bytebuff_free(&p_st_data);
    if (p_st_value && p_st_value->p_field_value)
        frfree(p_st_value->p_field_value);
    frfree(p_st_value);
    return;
}

static void __case6(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_IMAGE;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x01\x00\x00\x00\x01\x08", 6);

    i_ret = sqlserver_get_binary(p_st_data, i_whole_flg, u8_data_type, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRBLOB);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "BLOB_DATA");

    bytebuff_free(&p_st_data);
    if (p_st_value && p_st_value->p_field_value)
        frfree(p_st_value->p_field_value);
    frfree(p_st_value);
    return;
}

static void __case7(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_TEXT;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x01\x00\x00\x00\x01\x08", 6);

    i_ret = sqlserver_get_binary(p_st_data, i_whole_flg, u8_data_type, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    CU_ASSERT_PTR_NULL(p_st_value);

    bytebuff_free(&p_st_data);
    if (p_st_value && p_st_value->p_field_value)
        frfree(p_st_value->p_field_value);
    frfree(p_st_value);
    return;
}

static void __case8(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_BIGBINARY;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\xFF\xFF\x08", 8);

    i_ret = sqlserver_get_binary(p_st_data, i_whole_flg, u8_data_type, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_PTR_NOT_NULL(p_st_value);
    CU_ASSERT_EQUAL(p_st_value->em_data_type, FRBLOB);
    CU_ASSERT_PTR_NULL(p_st_value->p_field_value);

    bytebuff_free(&p_st_data);
    frfree(p_st_value);
    return;
}

static void __case9(void)
{
    int i_ret = 0;
    Frbyte_Packet *p_st_data = NULL;
    int i_whole_flg = FR_YES;
    uint8_t u8_data_type = TDS_DATA_TYPE_TEXT;
    Frdata_field *p_st_value = NULL;

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x01\x00\x00\x00\x01\x08", 6);

    i_ret = sqlserver_get_binary(p_st_data, i_whole_flg, u8_data_type, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);
    CU_ASSERT_PTR_NULL(p_st_value);

    bytebuff_free(&p_st_data);
    return;
}

int add_suite_sqlserver_get_binary(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" sqlserver_get_binary()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    //增加测试用例
    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL ||
        CU_add_test(pSuite, "case4", __case4) == NULL ||
        CU_add_test(pSuite, "case5", __case5) == NULL ||
        CU_add_test(pSuite, "case6", __case6) == NULL ||
        CU_add_test(pSuite, "case7", __case7) == NULL ||
        CU_add_test(pSuite, "case8", __case8) == NULL ||
        CU_add_test(pSuite, "case9", __case9) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
